package com.tanhua.api.mongo;

import com.tanhua.domain.mongo.FollowUser;
import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class VideoApiImpl implements VideoApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdService idService;

    //小视频分页查询
    public PageBeanVo findVideo(Integer page, Integer pagesize) {

        Query query = new Query();
        //排序
        query.with(new Sort(Sort.Direction.DESC,"created"));
        //分页
        query.skip((page-1)*pagesize).limit(pagesize);
        List<Video> list = mongoTemplate.find(query, Video.class);
        Long count = mongoTemplate.count(query, Video.class);
        return new PageBeanVo(page,pagesize,count,list);
    }

    //小视频的保存
    public void save(Video video) {
        //设置一个大数据使用到的唯一属性vid
        video.setVid(idService.getNextId("video"));
        //保存
        mongoTemplate.insert(video);
    }

    @Override
    public void saveGz(FollowUser followUser) {
        //保存
        mongoTemplate.insert(followUser);
    }

    //撤销关注
    public void removeGz(Long userId, Long followId) {
            // 条件
        Query query = new Query(Criteria.where("userId").is(userId)
        .and("followUserId").is(followId));
        // 删除
        mongoTemplate.remove(query,FollowUser.class);
    }

    @Override
    public PageBeanVo findVideoByUser(Integer page, Integer pagesize, Long uid) {

        Query query = new Query(Criteria.where("userId").is(uid));
        //排序
        query.with(new Sort(Sort.Direction.DESC,"created"));
        //分页
        query.skip((page-1)*pagesize).limit(pagesize);
        List<Video> list = mongoTemplate.find(query, Video.class);
        Long count = mongoTemplate.count(query, Video.class);

        return new PageBeanVo(page,pagesize,count,list);
    }

    @Override
    public Video findbyUid(String publishId) {
        return mongoTemplate.findById(new ObjectId(publishId), Video.class);
    }
    @Override
    public Video findbyUid(Long uid) {
        return mongoTemplate.findOne(new Query(Criteria.where("userId").is(uid)),Video.class);
    }

    @Override
    public Video findbyVid(long vid) {
        Query query=new Query(Criteria.where("vid").is(vid));
        return mongoTemplate.findOne(query,Video.class);
    }

    //查询关注列表
    public List<FollowUser> findFollowUserList(Long userId){
        Query query = new Query(Criteria.where("userId").is(userId));
        List<FollowUser> followUserList = mongoTemplate.find(query,FollowUser.class);
        return followUserList;
    }

    //查询所有该用户的视屏
    public List<Video> findVideoList(Long userId){
        Query query = new Query(Criteria.where("userId").is(userId));
        List<Video> videoList = mongoTemplate.find(query, Video.class);
        return videoList;
    }


}
